﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Common;
using MarginTrading.Services;
using System.IO;

namespace MarginTrading.Forms
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
       
        public static UserAccount currentUser = null;
        private static UInt64 uiSeqNO = 0;

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            LoadConnectionString();
            GenerateInitialSeqNO();

            LoginForm oForm = new LoginForm();
            oForm.ShowDialog();
            if (oForm.b_Canceled == true)
            {
                oForm.Close();
                return;
            }

            Application.Run(new MainWnd());
        }

        public static void LoadConnectionString()
        {
            try
            {
                using (StreamReader sr = new StreamReader("orasettings"))
                {
                    string settings = DESEncryptor.Decrypt(sr.ReadToEnd());

                    string[] array = settings.Split(' ');

                    string user = array[0];

                    if (user == null)
                    {
                        return;
                    }

                    string pass = array[1];

                    if (pass == null)
                    {
                        return;
                    }

                    string host = array[2];

                    if (host == null)
                    {
                        return;
                    }

                    string port = array[3];

                    if (port == null)
                    {
                        return;
                    }

                    string servicename = array[4];

                    if (servicename == null)
                    {
                        return;
                    }

                    Constants.DB_CONNECTION_STRING = "user id=" + user + ";password=" + pass + ";data source=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = " + host + ")(PORT = " + port + "))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = " + servicename + ")))";
                }
            }
            catch (System.Exception ex)
            {
            	
            }
        }

        private static void GenerateInitialSeqNO()
        {
            string sTimestamp = DateTime.Now.Ticks.ToString();
            sTimestamp = sTimestamp.Substring(2, 11);
            uiSeqNO = UInt64.Parse(sTimestamp);
        }

        public static UInt64 GetNextSeqNO()
        {
            return ++uiSeqNO;
        }
    }
}
